package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cheer.dto.ApplicantExportDTO;
import com.cheer.dto.ApplicantPageDTO;
import com.cheer.dto.ApplicantReviewPageDTO;
import com.cheer.entity.Applicant;
import com.cheer.vo.ApplicantReferenceListVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Mapper
@Repository
public interface ApplicantDao extends BaseMapper<Applicant> {

    default Applicant getByAccountId(Long accountId) {
        return selectOne(new QueryWrapper<Applicant>()
                .eq("account_id", accountId)
                .last("limit 1"));
    }

    default Applicant getByApplicantName(String applicantName) {
        return selectOne(new QueryWrapper<Applicant>()
                .eq("applicant_name", applicantName)
                .last("limit 1"));
    }

    default List<Applicant> listByOrgPath(String orgPath) {
        return selectList(new QueryWrapper<Applicant>()
                .likeRight("org_path", orgPath));
    }

    default List<Applicant> listByOrgPathOrJoinOrgPath(String orgPath) {
        return selectList(new QueryWrapper<Applicant>()
                .likeRight("org_path", orgPath)
                .or(applicantQueryWrapper -> {
                    applicantQueryWrapper.likeRight("join_review_org_path", orgPath);
                }));
    }

    default List<Applicant> listByOrgId(Long orgId) {
        return selectList(new QueryWrapper<Applicant>()
                .eq("org_id", orgId));
    }

    List<ApplicantPageDTO> listPage(@Param("orgPath") String orgPath, @Param("applicantName") String applicantName,
                                    @Param("contactPerson") String contactPerson, @Param("title") Integer title);


    List<ApplicantReviewPageDTO> listReviewPage(@Param("orgPath") String orgPath, @Param("applicantName") String applicantName,
                                                @Param("contactPerson") String contactPerson, @Param("status") Integer status,
                                                @Param("applicantStatus") Integer applicantStatus,
                                                @Param("submitted") Boolean submitted, @Param("reviewType") Integer reviewType,
                                                @Param("registerStatus") Integer registerStatus,
                                                @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    List<ApplicantReviewPageDTO> createSubmitAgainIdsPage( @Param("orgPath") String orgPath,
                                                             @Param("applicantIds") List<Long> applicantIds );



    List<ApplicantReferenceListVO> listReferencePage( @Param("orgPath") String orgPath,
                                                      @Param("vintage") Integer vintage,
                                                      @Param("isPost")  Integer isPost,
                                                      @Param("registerStatus") Integer registerStatus,
                                                      @Param("contactPerson") String contactPerson,
                                                      @Param("applicantName") String applicantName,
                                                      @Param("isTsw") Integer isTsw);


    List<ApplicantExportDTO> listExportApplicants(@Param("orgPath") String orgPath, @Param("applicantName") String applicantName,
                                                  @Param("contactPerson") String contactPerson, @Param("status") Integer status,
                                                  @Param("applicantStatus") Integer applicantStatus,
                                                  @Param("submitted") Boolean submitted, @Param("reviewType") Integer reviewType,
                                                  @Param("registerStatus") Integer registerStatus,
                                                  @Param("startTime") Date startTime, @Param("endTime") Date endTime);


    List<ApplicantReferenceListVO> submitAgainIdsPage(@Param("orgPath") String orgPath,@Param("referenceIds") List<Long> referenceIds);


    /**
     * 根据类型获取当前
     * @param registerStatus 文明号等级
     * @return
     */
    default List<Applicant> getByType( Integer registerStatus){

        QueryWrapper<Applicant> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Applicant::getRegisterStatus, registerStatus);
        return selectList(queryWrapper);
    }

}
